//https://leetcode.cn/problems/swap-nodes-in-pairs/

class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        if (head == nullptr || head->next == nullptr)
            return head;

        ListNode* prev = new ListNode(0);
        ListNode* newhead = prev;
        prev->next = head;
        ListNode* cur = head, * next = cur->next, * nnext = cur->next->next;

        while (cur && next)
        {
            //交换节点
            prev->next = next;
            next->next = cur;
            cur->next = nnext;

            //更新节点
            prev = cur;
            cur = nnext;
            if (cur) next = cur->next;
            if (next) nnext = next->next;
        }

        head = newhead->next;
        delete newhead;
        return head;

    }
};